<template>
  <div>
    <div style="margin-bottom: 10px">
      <div style="text-align: left;margin-bottom: 10px;margin-left: 10px">
        <span>个人信息</span>
      </div>
      <el-table
      v-loading="loading"
      :data="userInformation"
      border
      style="width: 100%">
      <el-table-column
          fixed
          prop="userId"
          label="编号"
          width="80">
      </el-table-column>
      <el-table-column
          prop="account"
          label="用户名"
          width="120">
      </el-table-column>
<!--      <el-table-column-->
<!--          prop="password"-->
<!--          label="密码"-->
<!--          width="120">-->
<!--      </el-table-column>-->
      <el-table-column
          prop="name"
          label="姓名"
          width="120">
      </el-table-column>
      <el-table-column
          prop="email"
          label="邮箱"
          width="120">
      </el-table-column>
      <el-table-column
          prop="phone"
          label="联系方式"
          width="120">
      </el-table-column>
      <el-table-column
          prop="question"
          label="密码问题"
          width="120">
      </el-table-column>
      <el-table-column
          prop="asw"
          label="问题答案"
          width="120">
      </el-table-column>
<!--      <el-table-column-->
<!--          prop="age"-->
<!--          label="年龄"-->
<!--          width="120">-->
<!--      </el-table-column>-->
      <el-table-column
          prop="sex"
          label="性别"
          width="120">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="edit1(scope.row)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
      </el-table>

      <el-dialog title="个人信息编辑" :visible.sync="dialogTableVisible2" width="400px">
        <el-form ref="registerForm" :model="userInformationItem" :rules="rules2">
          <el-form-item label="昵称" prop="name">
            <el-input v-model="userInformationItem.name"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userInformationItem.email"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="phone">
            <el-input v-model="userInformationItem.phone"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="infoEdit('registerForm')">立即修改</el-button>
            <el-button @click="resetForm('registerForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <div>
      <div style="text-align: left;margin-bottom: 10px;margin-left: 10px">
        <span>地址管理：</span>
      </div>
      <el-table
          v-loading="loading"
          :data="tableData"
          border
          style="width: 100%">
        <el-table-column
            fixed
            prop="id"
            label="编号"
            width="80">
        </el-table-column>
        <el-table-column
            prop="name"
            label="收件人姓名"
            width="120">
        </el-table-column>
        <el-table-column
            prop="mobile"
            label="收件人手机号码"
            width="120">
        </el-table-column>
        <el-table-column
            prop="province"
            label="省份"
            width="120">
        </el-table-column>
        <el-table-column
            prop="city"
            label="城市"
            width="120">
        </el-table-column>
        <el-table-column
            prop="district"
            label="区县"
            width="120">
        </el-table-column>
        <el-table-column
            prop="addr"
            label="详细地址"
            width="120">
        </el-table-column>
        <el-table-column
            prop="zip"
            label="邮编"
            width="120">
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button @click="edit2(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: right;margin-top: 20px">
        <el-button @click="openAddrAdd" size="small">添加地址</el-button>
      </div>
      <el-dialog title="地址添加" :visible.sync="dialogTableVisible" width="400px">
        <el-form ref="registerForm" :model="addrAddItem" :rules="rules">
          <el-form-item label="收件人姓名" prop="name">
            <el-input v-model="addrAddItem.name"></el-input>
          </el-form-item>
          <el-form-item label="收件人手机" prop="mobile">
            <el-input v-model.number="addrAddItem.mobile"></el-input>
          </el-form-item>
          <el-form-item label="省份" prop="province">
            <el-input v-model="addrAddItem.province" ></el-input>
          </el-form-item>
          <el-form-item label="城市" prop="city">
            <el-input v-model="addrAddItem.city" ></el-input>
          </el-form-item>
          <el-form-item label="区县" prop="district">
            <el-input v-model="addrAddItem.district"></el-input>
          </el-form-item>
          <el-form-item label="详细地址" prop="addr">
            <el-input v-model="addrAddItem.addr"></el-input>
          </el-form-item>
          <el-form-item label="邮编" prop="zip">
            <el-input v-model.number="addrAddItem.zip"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addrAdd('registerForm')">立即添加</el-button>
            <el-button @click="resetForm('registerForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "UserInformation",
  data(){
    return{
      tableData:null,
      userInformation:null,
      loading:true,
      dialogTableVisible: false,
      dialogTableVisible2:false,
      addrAddItem:{
        name:'',
        mobile:'',
        province:'',
        city:'',
        district:'',
        addr:'',
        zip:''
      },
      userInformationItem:{
        account:'',
        name:'',
        phone:'',
        email:''
      },
      rules: {
        name: [
          { required: true, message: '请输入收货人姓名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入收货人手机', trigger: 'blur' }
        ],
        addr: [
          { required: true, message: '请输入收货详细地址', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '请输入省份', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '请输入城市', trigger: 'blur' }
        ],
        district: [
          { required: true, message: '请输入区县', trigger: 'blur' }
        ],
        zip: [
          { required: true, message: '请输入邮编', trigger: 'blur' }
        ],
      },
      rules2: {
        name: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ]
      },
    }
  },
  methods:{
    getAddr(){
      const _this=this;
      axios.get('http://localhost:8181/user/findAddressByAccount/'+this.$store.state.account).then(function (resp){
        _this.tableData=resp.data.data;
        _this.loading=false;
      })
    },
    getUserInformation(){
      const _this=this;
      axios.get('http://localhost:8181/user/getUserInfo/'+this.$store.state.account).then(function (resp){
        _this.userInformation=resp.data.data;
        _this.loading=false;
      })
    },
    del(row){
      const _this=this;
      this.$confirm('此操作将永久删除该地址, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.post('http://localhost:8181/user/delAddress/'+row.id).then(function(resp){
          if(resp.data==0){
            _this.$message.error('删除失败!');
          }else{
            _this.$message({
              type: 'success',
              message: '删除成功!'
            });
            window.location.reload();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    edit1(row){
      console.log(this.userInformation);
      this.userInformationItem.account=this.$store.state.account;
      this.userInformationItem.name=this.userInformation[0].name;
      this.userInformationItem.phone=this.userInformation[0].phone;
      this.userInformationItem.email=this.userInformation[0].email;
      this.dialogTableVisible2=true;
    },
    openAddrAdd(){
      this.dialogTableVisible=true;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    addrAdd(formName){
      const _this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('http://localhost:8181/user/addAddress/'+this.$store.state.account,this.addrAddItem).then(function (resp){
            if(resp.data=='1'){
              _this.$message({
                message: '添加成功',
                type: 'success'
              });
              window.location.reload();
            }else{
              _this.$message({
                message: '添加失败',
                type: 'warning'
              });
            }
          })
        }
      });
    },
    infoEdit(formName){
      const _this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.userInformationItem);
          this.userInformation[0].name=this.userInformationItem.name;
          this.userInformation[0].phone=this.userInformationItem.phone;
          this.userInformation[0].email=this.userInformationItem.email;
          axios.post('http://localhost:8181/user/editUser',this.userInformation[0]).then(function (resp){
            if(resp.data=='1'){
              _this.$message({
                message: '修改成功',
                type: 'success'
              });
              window.location.reload();
            }else{
              _this.$message({
                message: '修改失败',
                type: 'warning'
              });
            }
          })
        }
      });
    }
  },
  created() {
    this.getUserInformation();
    this.getAddr();
  }
}
</script>

<style scoped>

</style>